package com.bootdo.shop.service.impl;

import java.nio.charset.Charset;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.bootdo.hjh.domain.ErrorConstant;
import com.bootdo.hjh.ex.ThrowUtils;
import com.bootdo.shop.dao.GoodsMapper;
import com.bootdo.shop.domain.Category;
import com.bootdo.shop.domain.GoodsCategory;
import com.bootdo.shop.domain.GoodsImage;
import com.bootdo.shop.domain.GoodsLimit;
import com.bootdo.shop.domain.GoodsSortConfig;
import com.bootdo.shop.domain.request.GoodsReq;
import com.bootdo.shop.domain.response.GoodsRes;
import com.bootdo.shop.domain.response.ShoppingCartRes;
import com.bootdo.shop.service.GoodsService;

@Service
public class GoodsServiceImpl implements GoodsService{

	private final static  Logger LOGGER = LoggerFactory.getLogger(GoodsServiceImpl.class);
	
	@Autowired
	private GoodsMapper goodsMapper;
	
	@Override
	public void subtractStock(Integer goodsId, Integer num, Integer specificationId) {
		GoodsRes gr = goodsMapper.getGoodsDetailById(goodsId);
		if(null == gr){
			ThrowUtils.throwException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "未找到该商品");
		}
		if(gr.getStockStatus() == 1){
			ThrowUtils.throwException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "该商品库存已经被锁定");
		}
		synchronized (this) {
			//是否需要减库存
			if(gr.getSubtract() == 1){
				if(gr.getAmount() >= num){
					goodsMapper.subtractStock(goodsId, num);
				}else{
					ThrowUtils.throwException(ErrorConstant.OPERATION_ERROR.getErrorCode(), gr.getTitle().concat(",库存不足,剩余").concat(gr.getAmount().toString()));
				}
			}
		}
	}

	@Override
	public void increaseStock(Integer goodsId, Integer num, Integer specificationId) {
		GoodsRes gr = goodsMapper.getGoodsDetailById(goodsId);
		if(null == gr){
			ThrowUtils.throwException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "未找到该商品");
		}
		goodsMapper.increaseStock(goodsId, num);
	}

	@Override
	public void updateGoodsBuyNumber(String orderNo) {
		goodsMapper.updateGoodsBuyCountByOrderNo(orderNo);
		
	}

	@Override
	public List<GoodsRes> getHomeList(GoodsReq goodsReq) {
		LOGGER.info("默认编码符:{}={}",Charset.defaultCharset(),System.getProperty("file.encoding"));
		List<GoodsRes> list = goodsMapper.getHomeList(goodsReq);
		return list;
	}

	@Override
	public GoodsRes goodsDetail(GoodsReq goodReq) {
		if(goodReq == null || goodReq.getId() == null){
			ThrowUtils.throwException(ErrorConstant.PARAM_ERROR.getErrorCode(), "商品ID不能为空");
		}
		GoodsRes goodsRes = goodsMapper.getGoodsDetailById(goodReq.getId());
		coverPric(goodReq.getMemberLevel(), goodReq.getAgentLevel(), goodsRes);
		if(goodsRes == null){
			ThrowUtils.throwException(ErrorConstant.OPERATION_ERROR.getErrorCode(), "未找到ID为"+goodReq.getId()+"的商品");
		}
		List<GoodsImage> goodsImageList = goodsMapper.getGoodsImageList(goodReq.getId());
		LOGGER.info("goodsImageList : {}",JSON.toJSONString(goodsImageList));
		goodsRes.setGoodsImageList(goodsImageList);
		//限购列表
		GoodsLimit goodsLimit = new GoodsLimit();
		goodsLimit.setGoodsId(goodReq.getId());
		List<GoodsLimit> goodsLimits = goodsMapper.getGoodsLimitList(goodsLimit);
		goodsRes.setGoodsLimits(goodsLimits);
		return goodsRes;
	}

	@Override
	public List<Category> categoryList() {
		List<Category> list = goodsMapper.categoryList();
		return list;
	}
	
	@Override
	public Category getCategoryById(Integer id) {
		return goodsMapper.getCategoryById(id);
	}

	@Override
	public List<GoodsCategory> getCategoryByGoodsId(Integer goodsId) {
		List<GoodsCategory> list = goodsMapper.getCategoryByGoodsId(goodsId);
		return list;
	}

	@Override
	public List<Category> categoryListByManager() {
		List<Category> list = goodsMapper.categoryListByManager();
		return list;
	}

	@Override
	public List<GoodsSortConfig> sortList() {
		return goodsMapper.sortList();
	}

	@Override
	public List<GoodsRes> goodsList(GoodsReq goodsReq) {
		if(goodsReq.getSortConfigId() != null){
			 GoodsSortConfig goodsSortConfig = goodsMapper.getGoodsSortConfig(goodsReq.getSortConfigId());
			 goodsReq.setSortConfig(goodsSortConfig.getConfig());
		}
		List<GoodsRes> list = goodsMapper.goodsList(goodsReq);
		coverPriceForGoods(goodsReq.getMemberLevel(), goodsReq.getAgentLevel(), list);
		return list;
	}
	
	@Override
	public Integer goodsListCount(GoodsReq goodsReq) {
		if(goodsReq.getSortConfigId() != null){
			 GoodsSortConfig goodsSortConfig = goodsMapper.getGoodsSortConfig(goodsReq.getSortConfigId());
			 goodsReq.setSortConfig(goodsSortConfig.getConfig());
		}
		return goodsMapper.goodsListCount(goodsReq);
	}

	@Override
	public GoodsRes getGoodsDetailById(Integer goodsId) {
		GoodsRes goodsRes = goodsMapper.getGoodsDetailById(goodsId);
		List<GoodsImage> goodsImageList = goodsMapper.getGoodsImageList(goodsId);
		goodsRes.setGoodsImageList(goodsImageList);
		return goodsRes;
	}

	@Override
	public String getStoreItemName(Integer storeItemId) {
		return goodsMapper.getStoreItemName(storeItemId);
	}

	@Override
	public Integer modifyCategory(Category category) {
		return goodsMapper.updateCategory(category);
	}

	@Override
	public Integer addCategory(Category category) {
		if(category.getSortOrder() == null) {
			category.setSortOrder(0);
		}
		if(category.getStatus() == null) {
			category.setStatus(1);
		}
		return goodsMapper.addCategory(category);
	}

	@Override
	public Integer updateGoods(GoodsReq goodsReq) {
		if(goodsReq.getSalePrice() == null) {
			goodsReq.setSalePrice(0);
		}
		if(goodsReq.getMarketPrice() == null) {
			goodsReq.setMarketPrice(0);
		}
		if(goodsReq.getIsFr() == null) {
			goodsReq.setIsFr(0);
		}
		goodsMapper.updateGoods(goodsReq);
		//更新图片
		if(goodsReq.getGoodsImageList() != null && !goodsReq.getGoodsImageList().isEmpty()) {
			for(GoodsImage gi : goodsReq.getGoodsImageList()) {
				gi.setGoodsId(goodsReq.getId());
				goodsMapper.deleteGoodsImage(gi.getGoodsId(), gi.getSortOrder());
				goodsMapper.insertGoodsImage(gi);
			}
		}
		
		//更新分类
		if(null != goodsReq.getCategoryIdList() && !goodsReq.getCategoryIdList().isEmpty()) {
			goodsMapper.deleteGoodsCategoryByGoodsId(goodsReq.getId());
			for(Integer categoryId  : goodsReq.getCategoryIdList()) {
				goodsMapper.insertGoodsCategory(goodsReq.getId(), categoryId);
			}
		}
		//删除所有限购
		goodsMapper.deleteGoodsLimit(goodsReq.getId());
		if(goodsReq.getGoodsLimitList() != null && goodsReq.getGoodsLimitList().size() > 0) {
			for(GoodsLimit goodsLimit : goodsReq.getGoodsLimitList()) {
				if(goodsLimit != null && goodsLimit.getLimitType() != null && goodsLimit.getLimitCount() != null 
						&& goodsLimit.getLimitCount() > 0) {
					goodsLimit.setGoodsId(goodsReq.getId());
					goodsMapper.addGoodsLimit(goodsLimit);
				}
			}
		}
		return null;
	}

	@Override
	public Integer addGoods(GoodsReq goodsReq) {
		if(goodsReq.getSalePrice() == null) {
			goodsReq.setSalePrice(0);
		}
		if(goodsReq.getMarketPrice() == null) {
			goodsReq.setMarketPrice(0);
		}
		if(goodsReq.getIsFr() == null) {
			goodsReq.setIsFr(0);
		}
		goodsMapper.addGoods(goodsReq);
		//更新图片
		if(goodsReq.getGoodsImageList() != null && !goodsReq.getGoodsImageList().isEmpty()) {
			for(GoodsImage gi : goodsReq.getGoodsImageList()) {
				gi.setGoodsId(goodsReq.getId());
				goodsMapper.deleteGoodsImage(gi.getGoodsId(), gi.getSortOrder());
				goodsMapper.insertGoodsImage(gi);
			}
		}
		//更新分类
		if(null != goodsReq.getCategoryIdList() && !goodsReq.getCategoryIdList().isEmpty()) {
			goodsMapper.deleteGoodsCategoryByGoodsId(goodsReq.getId());
			for(Integer categoryId  : goodsReq.getCategoryIdList()) {
				goodsMapper.insertGoodsCategory(goodsReq.getId(), categoryId);
			}
		}
		return null;
	}

	@Override
	public Integer deleteImage(GoodsImage goodsImage) {
		return goodsMapper.deleteGoodsImage(goodsImage.getGoodsId(), goodsImage.getSortOrder());
	}
	

	@Override
	public List<GoodsLimit> getGoodsLimit(Integer goodsId) {
		return goodsMapper.getGoodsLimit(goodsId);
	}

	@Override
	public void coverPriceForGoods(Integer memberLevel, Integer agentLevel, List<GoodsRes> goodsList) {
		if((memberLevel == null && agentLevel == null ) || null == goodsList || goodsList.isEmpty()) {
			return ;
		}
		for(GoodsRes goods : goodsList) {
			if(agentLevel != null && agentLevel > 0 ) {
				switch (agentLevel) {
				case 1:
					goods.setSalePrice(goods.getAgent1Price());
					break;
				case 2:
					goods.setSalePrice(goods.getAgent2Price());
					break;
				case 3:
					goods.setSalePrice(goods.getAgent3Price());
					break;
				case 4:
					goods.setSalePrice(goods.getAgent4Price());
					break;
				case 5:
					goods.setSalePrice(goods.getAgent5Price());
					break;
				default:
					break;
				}
			}else {
				switch (memberLevel) {
				case 1:
					goods.setSalePrice(goods.getGeneralUserPrice());
					break;
				case 2:
					goods.setSalePrice(goods.getPlatinaUserPrice());
					break;
				case 3:
					goods.setSalePrice(goods.getVipUserPrice());
					break;
				default:
					break;
				}
			}
		}
		
	}
	
	
	public void coverPric(Integer memberLevel, Integer agentLevel, GoodsRes goods) {
		
		if(agentLevel != null && agentLevel > 0 ) {
			switch (agentLevel) {
			case 1:
				goods.setSalePrice(goods.getAgent1Price());
				break;
			case 2:
				goods.setSalePrice(goods.getAgent2Price());
				break;
			case 3:
				goods.setSalePrice(goods.getAgent3Price());
				break;
			case 4:
				goods.setSalePrice(goods.getAgent4Price());
				break;
			case 5:
				goods.setSalePrice(goods.getAgent5Price());
				break;
			default:
				break;
			}
		}else {
			switch (memberLevel) {
			case 1:
				goods.setSalePrice(goods.getGeneralUserPrice());
				break;
			case 2:
				goods.setSalePrice(goods.getPlatinaUserPrice());
				break;
			case 3:
				goods.setSalePrice(goods.getVipUserPrice());
				break;
			default:
				break;
			}
		}
	}

	@Override
	public void coverPriceFprCart(Integer memberLevel, Integer agentLevel, List<ShoppingCartRes> shoppingCartList) {
		if(null == shoppingCartList || shoppingCartList.isEmpty()) {
			return ;
		}
		for(ShoppingCartRes goods : shoppingCartList) {
			if(agentLevel != null && agentLevel > 0 ) {
				switch (agentLevel) {
				case 1:
					goods.setSalePrice(goods.getAgent1Price());
					goods.setShopCredit(goods.getAgent1Credits());
					break;
				case 2:
					goods.setSalePrice(goods.getAgent2Price());
					goods.setShopCredit(goods.getAgent2Credits());
					break;
				case 3:
					goods.setSalePrice(goods.getAgent3Price());
					goods.setShopCredit(goods.getAgent3Credits());
					break;
				case 4:
					goods.setSalePrice(goods.getAgent4Price());
					goods.setShopCredit(goods.getAgent4Credits());
					break;
				case 5:
					goods.setSalePrice(goods.getAgent5Price());
					goods.setShopCredit(goods.getAgent5Credits());
					break;
				default:
					break;
				}
			}else {
				switch (memberLevel) {
				case 1:
					goods.setSalePrice(goods.getGeneralUserPrice());
					goods.setShopCredit(goods.getGeneralUserCredits());
					break;
				case 2:
					goods.setSalePrice(goods.getPlatinaUserPrice());
					goods.setShopCredit(goods.getPlatinaUserCredits());
					break;
				case 3:
					goods.setSalePrice(goods.getVipUserPrice());
					goods.setShopCredit(goods.getVipUserCredits());
					break;
				default:
					break;
				}
			}
		}
		
	}
	
	
	
	
}